﻿// 员工数据分析.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
#include <map>

//c++17 

struct Employee {
    std::string name;
    std::string department;
    int age;
    double salary;
};

void employeeAnalysisDemo() {
    std::vector<Employee> employees = {
        {"Alice", "Engineering", 32, 85000},
        {"Bob", "Marketing", 28, 62000},
        {"Charlie", "Engineering", 45, 115000},
        {"Diana", "HR", 35, 75000},
        {"Eve", "Marketing", 31, 68000}
    };

    // 多条件排序：先按部门，再按工资降序
    std::sort(employees.begin(), employees.end(),
        [](const Employee& a, const Employee& b) {//如果写成 Employee emp，每次调用 lambda 时，Employee 对象会被拷贝一份，增加开销
            if (a.department != b.department)
                return a.department < b.department;
            return a.salary > b.salary;
        });

    // 查找高薪年轻员工
    auto it = std::find_if(employees.begin(), employees.end(),
        [](const Employee& emp) {
            return emp.age < 35 && emp.salary > 70000;
        });

    // 统计各部门人数
    std::map<std::string, int> deptCount;
    for (const auto& emp : employees) {//  auto 自动推断类型 
		deptCount[emp.department]++;//根据部门名称统计人数
    }

    // 打印结果
    std::cout << "Sorted employees:\n";
	std::cout << std::left << std::setw(15) << "Name"//setw 设置宽度，left 设置左对齐
        << std::setw(15) << "Department"
        << std::setw(5) << "Age"
        << "Salary\n";

    for (const auto& emp : employees) {
        std::cout << std::setw(15) << emp.name
            << std::setw(15) << emp.department
            << std::setw(5) << emp.age
            << emp.salary << "\n";
    }

    if (it != employees.end()) {
        std::cout << "\nHigh-salary young employee found: " << it->name << "\n";
    }

    std::cout << "\nDepartment counts:\n";
    for (const auto& [dept, count] : deptCount) {
        std::cout << dept << ": " << count << "\n";
    }
}

int main() {
    employeeAnalysisDemo();
    return 0;
}